package com.hod.roommap.client.model;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.ListBox;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.Record;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.FieldSet;
import com.gwtext.client.widgets.form.Label;
import com.gwtext.client.widgets.form.NumberField;
import com.gwtext.client.widgets.form.TextField;
import com.hod.roommap.client.HOD;


public class BedFormPanel extends HODForm {
	
	protected final NumberField txtidBed  = new NumberField ("idBed","idBed",150); 																		 
  	protected final NumberField txtidRoom  = new NumberField ("idRoom","idRoom",150);
  	protected final NumberField txtNumber  = new NumberField ("number","number",150);
  	protected final ListBox txtState = new ListBox();
	protected final ListBox txtBedType  = new ListBox();


	public BedFormPanel(GearsDataStore ds) {
		super(ds);
		add(createFieldSet());        
        createButtons();
	}
	
	@Override
	protected void createButtons() {
 		 ButtonListenerAdapter savelistener = new ButtonListenerAdapter() {
              public void onClick(Button button, EventObject e) {
              	try {
              		Bed o = null;
              		if (txtidBed.getText()==""){
              			o = new Bed();
              			o.setIdBed(null);
              		 }else{
              			o = (Bed) ds.findById(new Integer(txtidBed.getText()));
              		 }                		
                      //metodos para setear el resto de los campos del objeto                       
                      o.setIdRoom(txtidRoom.getValue().intValue());
                      o.setIdBedType(Integer.valueOf(txtBedType.getValue(txtBedType.getSelectedIndex())));
                      o.setState(txtState.getItemText(txtState.getSelectedIndex()));
                      ds.save(o);
                      HOD.reload();
 					} catch (Exception e1) {
 						// TODO Auto-generated catch block
 						Window.alert(e1.getMessage());
 					}
              }
          };
          ButtonListenerAdapter deletelistener = new ButtonListenerAdapter() {
              public void onClick(Button button, EventObject e) {
              	try {
              		Bed o = (Bed) ds.findById(new Integer(txtidBed.getText()));
              		ds.delete(o);
              		HOD.reload();
 					} catch (Exception e1) {
 						// TODO Auto-generated catch block
 						Window.alert(e1.getMessage());
 					}
              }
          };
          ButtonListenerAdapter createlistener = new ButtonListenerAdapter() {
              public void onClick(Button button, EventObject e) {
              	try {
              		getForm().reset();
 					} catch (Exception e1) {
 						// TODO Auto-generated catch block
 						Window.alert(e1.getMessage());
 					}
              }
          };
          	           
          
          Button save = new Button("Save", savelistener);
          Button delete = new Button("Delete", deletelistener);
 		  Button newRecord = new Button("New", createlistener);
          Button cancel = new Button("Cancel");
 
          addButton(save);
          addButton(delete);
          addButton(cancel);
          addButton(newRecord);
 		
 	}

	@Override
	protected FieldSet createFieldSet() {
  		// TODO Auto-generated method stub
		
		  txtState.addItem("FREE");
          txtState.addItem("OCCUPIED");
          txtState.addItem("RESERVED");
  		  
          FieldSet fieldSet = new FieldSet();
          fieldSet.setLabelWidth(80);
          fieldSet.setTitle("Bed");
          fieldSet.setAutoHeight(true);
          fieldSet.setBorder(false);
  
          //agregar el resto de los campos
          fieldSet.add(txtidBed);
          fieldSet.add(txtidRoom);
          fieldSet.add(txtBedType);
          fieldSet.add(new Label("Estado"));
          fieldSet.add(txtState);
          
          txtidBed.setAllowDecimals(false);
          txtidRoom.setAllowDecimals(false);
          
          txtidBed.setHideLabel(true);
          txtidBed.setVisible(false);
          txtidRoom.setHideLabel(true);
          txtidRoom.setVisible(false);
          
          BedTypeCRUD.createListBox(0, txtBedType);
          	          	
          return fieldSet;
  		
  	}

	@Override
	public void updateDataControls(Record record) {
		// TODO Auto-generated method stub
		getForm().loadRecord(record);  	
	}
	
	public void setIdRoom(int idRoom) {
		this.txtidRoom.setValue(idRoom);		
	}
	
	public void setIdBedType(int indice){		
		for (short i=0;i<txtBedType.getItemCount();i++){
			if (indice==Integer.valueOf(txtBedType.getValue(i))){
			   txtBedType.setSelectedIndex(i);
			   break;
			}   
		}
	}

	@Override
	public void setObject(GearsDataObject object) {
		// TODO Auto-generated method stub
		Bed bed = (Bed)object;
		int bedType = 0;
		this.txtidBed.setValue(bed.getIdBed());
		this.txtidRoom.setValue(bed.getIdRoom());
		for (int i=0;i<txtBedType.getItemCount();i++){
			bedType = Integer.valueOf(txtBedType.getValue(i));
		    if (bedType==bed.getIdBedType()){
		       txtBedType.setItemSelected(i, true);
		       break;
		    }
		}
		for (int i=0;i<txtState.getItemCount();i++)
		    if (bed.getState().compareTo(txtState.getItemText(i))==0){
		    	txtState.setItemSelected(i, true);
		    	break;
		    }
	}

}
